package com.tuanzi.loan.business.service.family.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.family.ProductMatchRule;
import com.tuanzi.loan.business.repository.family.ProductMatchRuleRepository;
import com.tuanzi.loan.business.service.family.ProductMatchRuleService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.exception.ValidateException;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.ProductMatchRuleSearchRequest;
import com.tuanzi.loan.vo.response.family.ProductMatchRuleSearchResponse;
import com.tuanzi.loan.vo.view.family.ProductMatchRuleView;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class ProductMatchRuleServiceImpl extends BaseAuditServiceImpl<ProductMatchRule, Long> implements ProductMatchRuleService {

	@Autowired
	private ProductMatchRuleRepository productMatchRuleRepository;

	@Override
	protected JPAAuditRepository<ProductMatchRule, Long> getRepository() {
		return productMatchRuleRepository;
	}

	@Override
	public Page<ProductMatchRuleSearchResponse> list(PageSearcher<ProductMatchRuleSearchRequest> pageSearcher) {
		return productMatchRuleRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}

	@Override
	public ProductMatchRuleView loadProductMatchRuleView(Long productMatchRuleId) {
		ProductMatchRuleView view = new ProductMatchRuleView();

		ProductMatchRule productMatchRule = productMatchRuleRepository.findOne(productMatchRuleId);
		BeanUtils.copyProperties(productMatchRule, view);

		view.setItem(GsonUtils.toObject(productMatchRule.getItemJson(), ProductMatchRuleView.ProductMatchRuleItem.class));
		view.setPriority(GsonUtils.toObject(productMatchRule.getPriorityJson(), ProductMatchRuleView.ProductMatchRulePriority.class));
		view.setConfig(GsonUtils.toObject(productMatchRule.getConfigJson(), ProductMatchRuleView.ProductMatchRuleConfig.class));

		return view;
	}

	@Override
	public ProductMatchRuleView saveOrUpdateProductMatchRuleView(ProductMatchRuleView view) {
		ProductMatchRule productMatchRule = null;

		if (view.getId() != null) {
			productMatchRule = productMatchRuleRepository.findOne(view.getId());
		} else {
			productMatchRule = new ProductMatchRule();
		}

		BeanUtils.copyProperties(view, productMatchRule);

		ProductMatchRule dbProductMatchRule = productMatchRuleRepository.findByAdvertPositionAndRecommendPosition(productMatchRule.getAdvertPosition(),
				productMatchRule.getRecommendPosition());
		if (dbProductMatchRule != null && (productMatchRule.getId() == null || !productMatchRule.getId().equals(dbProductMatchRule.getId()))) {
			throw new ValidateException("该推荐位和广告位已存在匹配规则");
		}

		productMatchRule.setItemJson(GsonUtils.toJson(view.getItem()));
		productMatchRule.setPriorityJson(GsonUtils.toJson(view.getPriority()));
		productMatchRule.setConfigJson(GsonUtils.toJson(view.getConfig()));

		saveOrUpdate(productMatchRule);
		view.setId(productMatchRule.getId());
		return view;
	}
}
